import request from "@/utils/request";
export interface IUserParams {
  username: string;
  password: string;
  autoLogin?: boolean;
}
export const userLogin = (params: IUserParams) => {
  return request.post("login", params);
};

// 个人设置
export interface UserInfoType {
  username: string;
  objectId: string;
  sessionToken?: string;
  avatar?: string;
  checkedKeys: Array<string>;
  roleId: string;
  roleName: string;
}
export const userUpdate = (userId: string, body: UserInfoType) => {
  return request.put(`users/${userId}`, body);
};

// 角色新增
export interface RoleType {
  objectId: string;
  roleName: string;
  checkedKeys: Array<string>;
  halfCheckedKeys: Array<string>;
  idx: number;
}
export const rolePost = (roleObj: RoleType) => {
  return request.post("classes/ReactRole1", roleObj);
};

// 角色列表
export const roleGet = (roleId: string = "") => {
  let roleStr = roleId ? `/${roleId}` : "";
  return request.get(`classes/ReactRole1${roleStr}`);
};

// 角色更新
export const rolePut = (roleId: string, roleObj: RoleType) => {
  return request.put(`classes/ReactRole1/${roleId}`, roleObj);
};

// 角色删除
export const roleDel = (roleId: string) => {
  return request.delete(`classes/ReactRole1/${roleId}`);
};

// 批量删除角色
export const roleBatchDel = (selectedRowKeys: Array<string>) => {
  let requests = selectedRowKeys.map((roleId) => {
    return {
      method: "DELETE",
      path: `/1.1/classes/ReactRole1/${roleId}`,
    };
  });
  return request.post("batch", { requests });
};

//账号列表
export const userGet = () => {
  return request.get("users");
};

// 账号分配（帮别人注册）
export interface UserRoleType extends IUserParams {
  roleId: string;
  idx: number;
  roleName: string;
}
export const userPost = (userObj: UserRoleType) => {
  return request.post("users", userObj);
};
